<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        /* 页面基础样式，简洁明了 */
        :root {
            --bg: #0b1220;
            --card: #111a2b;
            --muted: #aab3c5;
            --primary: #4f8cff;
            --danger: #ff5d5d;
            --ok: #36c896;
        }
        html, body {
            height: 100%;
            margin: 0;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
            background: radial-gradient(1200px 1200px at 80% -20%, #12203a 0%, #0b1220 60%);
            color: #e6ebf3;
        }
        .wrap {
            min-height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 24px;
        }
        .card {
            width: 100%;
            max-width: 420px;
            background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
            border: 1px solid rgba(255,255,255,0.08);
            border-radius: 14px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.35);
            padding: 24px 24px 18px;
            backdrop-filter: blur(6px);
        }
        .title {
            font-size: 20px;
            font-weight: 700;
            margin-bottom: 8px;
        }
        .subtitle {
            font-size: 13px;
            color: var(--muted);
            margin-bottom: 20px;
        }
        .field {
            display: flex;
            flex-direction: column;
            margin-bottom: 14px;
        }
        .label {
            font-size: 13px;
            margin-bottom: 6px;
            color: #cfd6e4;
        }
        .input {
            padding: 10px 12px;
            border-radius: 10px;
            border: 1px solid rgba(255,255,255,0.12);
            outline: none;
            background: rgba(0,0,0,0.25);
            color: #e6ebf3;
            transition: border 0.2s ease;
        }
        .input:focus {
            border-color: rgba(79,140,255,0.8);
        }
        .btn {
            width: 100%;
            padding: 11px 14px;
            border-radius: 10px;
            border: none;
            background: linear-gradient(90deg, #4f8cff, #6aa8ff);
            color: #fff;
            font-weight: 600;
            cursor: pointer;
            margin-top: 6px;
        }
        .btn.secondary {
            background: rgba(255,255,255,0.08);
            color: #e6ebf3;
        }
        .row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-top: 10px;
        }
        .msg {
            margin-top: 14px;
            font-size: 13px;
            color: var(--muted);
            white-space: pre-wrap;
        }
        .ok { color: var(--ok); }
        .err { color: var(--danger); }
        .token {
            margin-top: 10px;
            font-size: 12px;
            color: #9fb0d1;
            word-break: break-all;
            background: rgba(255,255,255,0.04);
            border: 1px solid rgba(255,255,255,0.08);
            padding: 10px;
            border-radius: 10px;
        }
    </style>
</head>
<body>
    <!-- 登录卡片 -->
    <div class="wrap">
        <div class="card">
            <div class="title">登录</div>
            <!-- <div class="subtitle">使用用户名和密码获取 JWT，用于访问受保护接口</div> -->

            <div class="field">
                <label class="label">用户名</label>
                <input id="username" class="input" placeholder="请输入用户名" autocomplete="username">
            </div>
            <div class="field">
                <label class="label">密码</label>
                <input id="password" type="password" class="input" placeholder="请输入密码" autocomplete="current-password">
            </div>
            <button id="loginBtn" class="btn">登录</button>

            <!-- <div class="row">
                <button id="checkBtn" class="btn secondary">验证受保护接口</button>
                <button id="logoutBtn" class="btn secondary">登出</button>
            </div> -->

            <div id="message" class="msg"></div>
            <div id="tokenBox" class="token" style="display:none;"></div>
        </div>
    </div>

    <script>
        /* 常量区：后端接口地址前缀，可根据部署修改 */
        const API_BASE = '';

        /* 工具函数：显示消息 */
        function showMsg(text, type) {
            const el = document.getElementById('message');
            el.className = 'msg ' + (type || '');
            el.textContent = text || '';
        }

        /* 工具函数：显示或隐藏 token */
        function showToken(token) {
            const box = document.getElementById('tokenBox');
            if (token) {
                box.style.display = 'block';
                box.textContent = token;
            } else {
                box.style.display = 'none';
                box.textContent = '';
            }
        }

        /* 从 localStorage 加载 token */
        const existing = localStorage.getItem('jwt_token');
        if (existing) {
            showToken(existing);
            showMsg('已从本地加载登录凭证', 'ok');
        }

        /* 登录事件处理 */
        document.getElementById('loginBtn').addEventListener('click', async () => {
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            if (!username || !password) {
                showMsg('用户名和密码不能为空', 'err');
                return;
            }
            showMsg('正在登录中…');
            try {
                const res = await fetch(API_BASE + '/api/auth/signin', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ username, password })
                });
                if (!res.ok) {
                    const txt = await res.text();
                    showMsg('登录失败 ' + res.status + ' ' + txt, 'err');
                    return;
                }
                const data = await res.json();
                const token = data && data.data && data.data.token;
                if (!token) {
                    showMsg('登录成功，但未获取到 token，请检查后端返回结构', 'err');
                    return;
                }
                localStorage.setItem('jwt_token', token);
                showToken(token);
                showMsg('登录成功，即将跳转到首页…', 'ok');
                setTimeout(() => { window.location.href = 'index.html'; }, 600);
            } catch (e) {
                showMsg('登录异常：' + e.message, 'err');
            }
        });

        /* 验证受保护接口 */
        document.getElementById('checkBtn').addEventListener('click', async () => {
            const token = localStorage.getItem('jwt_token');
            if (!token) {
                showMsg('未登录或 token 不存在', 'err');
                return;
            }
            showMsg('正在请求受保护接口…');
            try {
                const res = await fetch(API_BASE + '/api/test/user', {
                    headers: { 'Authorization': 'Bearer ' + token }
                });
                const txt = await res.text();
                if (!res.ok) {
                    showMsg('访问失败 ' + res.status + ' ' + txt, 'err');
                } else {
                    showMsg('访问成功：' + txt, 'ok');
                }
            } catch (e) {
                showMsg('请求异常：' + e.message, 'err');
            }
        });

        /* 登出并清理本地凭证 */
        document.getElementById('logoutBtn').addEventListener('click', () => {
            localStorage.removeItem('jwt_token');
            showToken('');
            showMsg('已登出并清除本地 token', 'ok');
        });
    </script>
</body>
</html>


